﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Table_Dal;
namespace ApplicationLayer.Obj_frmCaNhan
{
    public partial class frmChinhsuaCauhoi : DevComponents.DotNetBar.OfficeForm
    {
        string cboMa1 = "", cboMa2 = "";
        public frmChinhsuaCauhoi(string s1, string s2)
        {
            cboMa1 = s1;
            cboMa2 = s2;
            InitializeComponent();
        }
        string MacauHoi = "";
        public frmChinhsuaCauhoi(string Ma)
        {
            MacauHoi = Ma;
            InitializeComponent();
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        string MaCauA = "";
        string MaCauB = "";
        string MaCauC = "";
        string MaCauD = "";
        string MaCauE = "";
        string MaCauF = "";
        public void Hide_TaoMoi()
        {
            btn_XoaA.Visible = false;
            btn_XoaB.Visible = false;
            btn_XoaC.Visible = false;
            btn_XoaD.Visible = false;
            btn_XoaE.Visible = false;
            btn_XoaF.Visible = false;

            Link_ClearA.Visible = false;
            Link_ClearB.Visible = false;
            Link_ClearC.Visible = false;
            Link_ClearD.Visible = false;
            Link_ClearE.Visible = false;
            Link_ClearF.Visible = false;
            Link_ClearNoidung.Visible = false;

            pic_A.Visible = false;
            pic_B.Visible = false;
            pic_C.Visible = false;
            pic_D.Visible = false;
            pic_E.Visible = false;
            pic_F.Visible = false;
            pic_Noidung.Visible = false;

            pnlDapanE.Visible = false;
            pnlDapanF.Visible = false;
            pnlThem.Visible = false;
        }
        public void Load_Noidung()
        {
            CauHoiBll chbll = new CauHoiBll();
            CauHoiTbl ch = new CauHoiTbl();
            ch = chbll.ThongTinCauHoi_TheoMaCauHoi_TenCauHoi(MacauHoi,"");
            txtNoidung.Text = ch.NoiDung;
            if (ch.HinhAnh == "" ||ch.HinhAnh==null)
            {
                pic_Noidung.Visible = false;
                Link_ClearNoidung.Visible = false;
            }
            else
            {
                pic_Noidung.Visible = true;
                Link_ClearNoidung.Visible = true;
                pic_Noidung.ImageLocation = ch.HinhAnh;
            }
            RatingDoKho.Rating = ch.DoKho;

            cboMin.Text = ch.ThoiGian.ToString();
        }
        public void Load_DapAn()
        {
            int soDapan = 0;
            DapAnBll dapan = new DapAnBll();
            List<DapAnTbl> DsdapAn = new List<DapAnTbl>();
            DsdapAn = dapan.DapAnCauHoi_TheoMacauHoi_TenCauHoi(MacauHoi, "");
            soDapan = DsdapAn.Count;
            if (soDapan < 2)
                return;
            
            if (soDapan == 2)
            {
                pnlDapanC.Visible = false;
                pnlDapanD.Visible = false;
                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;

                txtDapanA.Text = DsdapAn[0].NoiDung;
                MaCauA = DsdapAn[0].MaDapAn;
                if (DsdapAn[0].LoaiDA == 1)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                if (DsdapAn[0].HinhAnh == "" || DsdapAn[0].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_A.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_A.Visible = true;
                    Link_ClearA.Visible = true;
                }
                txtDapanB.Text = DsdapAn[1].NoiDung;
                MaCauB = DsdapAn[1].MaDapAn;
                if (DsdapAn[1].LoaiDA == 1)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                if (DsdapAn[1].HinhAnh == "" || DsdapAn[1].HinhAnh == null)
                {
                    Link_ClearB.Visible = false;
                    pic_B.Visible = false;
                }
                else
                {
                    pic_B.ImageLocation = DsdapAn[1].HinhAnh;
                    pic_B.Visible = true;
                    Link_ClearB.Visible = true;
                }
            }
            if (soDapan == 3)
            {
                pnlDapanD.Visible = false;
                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;

                txtDapanA.Text = DsdapAn[0].NoiDung;
                MaCauA = DsdapAn[0].MaDapAn;
                if (DsdapAn[0].LoaiDA == 1)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                if (DsdapAn[0].HinhAnh == "" || DsdapAn[0].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_A.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_A.Visible = true;
                    Link_ClearA.Visible = true;
                }
                txtDapanB.Text = DsdapAn[1].NoiDung;
                MaCauB = DsdapAn[1].MaDapAn;

                if (DsdapAn[1].LoaiDA == 1)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                if (DsdapAn[1].HinhAnh == "" || DsdapAn[1].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_B.ImageLocation = DsdapAn[1].HinhAnh;
                    pic_B.Visible = true;
                    Link_ClearB.Visible = true;
                }
                txtDapanC.Text = DsdapAn[2].NoiDung;
                MaCauC = DsdapAn[2].MaDapAn;
                if (DsdapAn[2].LoaiDA == 1)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                if (DsdapAn[2].HinhAnh == "" || DsdapAn[2].HinhAnh == null)
                {
                    Link_ClearC.Visible = false;
                    pic_C.Visible = false;
                }
                else
                {
                    pic_C.ImageLocation = DsdapAn[2].HinhAnh;
                    pic_C.Visible = true;
                    Link_ClearC.Visible = true;
                }
            }
            if (soDapan == 4)
            {
                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;

                txtDapanA.Text = DsdapAn[0].NoiDung;
                MaCauA = DsdapAn[0].MaDapAn;
                if (DsdapAn[0].LoaiDA == 1)
                {

                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                if (DsdapAn[0].HinhAnh == "" || DsdapAn[0].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_A.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_A.Visible = true;
                    Link_ClearA.Visible = true;
                }
                txtDapanB.Text = DsdapAn[1].NoiDung;
                MaCauB = DsdapAn[1].MaDapAn;
                if (DsdapAn[1].LoaiDA == 1)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                if (DsdapAn[1].HinhAnh == "" || DsdapAn[1].HinhAnh == null)
                {
                    Link_ClearB.Visible = false;
                    pic_B.Visible = false;
                }
                else
                {
                    pic_B.ImageLocation = DsdapAn[1].HinhAnh;
                    pic_B.Visible = true;
                    Link_ClearB.Visible = true;
                }
                txtDapanC.Text = DsdapAn[2].NoiDung;
                MaCauC = DsdapAn[2].MaDapAn;
                if (DsdapAn[2].LoaiDA == 1)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                if (DsdapAn[2].HinhAnh == "" || DsdapAn[2].HinhAnh == null)
                {
                    Link_ClearC.Visible = false;
                    pic_C.Visible = false;
                }
                else
                {
                    pic_C.ImageLocation = DsdapAn[2].HinhAnh;
                    pic_C.Visible = true;
                    Link_ClearC.Visible = true;
                }
                txtDapanD.Text = DsdapAn[3].NoiDung;
                MaCauD = DsdapAn[3].MaDapAn;
                if (DsdapAn[3].LoaiDA == 1)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                if (DsdapAn[3].HinhAnh == "" || DsdapAn[3].HinhAnh == null)
                {
                    Link_ClearD.Visible = false;
                    pic_D.Visible = false;
                }
                else
                {
                    pic_D.ImageLocation = DsdapAn[3].HinhAnh;
                    pic_D.Visible = true;
                    Link_ClearD.Visible = true;
                }
            }
            if (soDapan == 5)
            {
                pnlDapanF.Visible = false;

                txtDapanA.Text = DsdapAn[0].NoiDung;
                MaCauA = DsdapAn[0].MaDapAn;
                if (DsdapAn[0].LoaiDA == 1)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                if (DsdapAn[0].HinhAnh == "" || DsdapAn[0].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_A.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_A.Visible = true;
                    Link_ClearA.Visible = true;
                }
                txtDapanB.Text = DsdapAn[1].NoiDung;
                MaCauB = DsdapAn[1].MaDapAn;
                if (DsdapAn[1].LoaiDA == 1)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                if (DsdapAn[1].HinhAnh == "" || DsdapAn[1].HinhAnh == null)
                {
                    Link_ClearB.Visible = false;
                    pic_B.Visible = false;
                }
                else
                {
                    pic_B.ImageLocation = DsdapAn[1].HinhAnh;
                    pic_B.Visible = true;
                    Link_ClearB.Visible = true;
                }
                txtDapanC.Text = DsdapAn[2].NoiDung;
                MaCauC = DsdapAn[2].MaDapAn;
                if (DsdapAn[2].LoaiDA == 1)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                if (DsdapAn[2].HinhAnh == "" || DsdapAn[2].HinhAnh == null)
                {
                    Link_ClearC.Visible = false;
                    pic_C.Visible = false;
                }
                else
                {
                    pic_C.ImageLocation = DsdapAn[2].HinhAnh;
                    pic_C.Visible = true;
                    Link_ClearC.Visible = true;
                }
                txtDapanD.Text = DsdapAn[3].NoiDung;
                MaCauD = DsdapAn[3].MaDapAn;
                if (DsdapAn[3].LoaiDA == 1)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                if (DsdapAn[3].HinhAnh == "" || DsdapAn[3].HinhAnh == null)
                {
                    Link_ClearD.Visible = false;
                    pic_D.Visible = false;
                }
                else
                {
                    pic_D.ImageLocation = DsdapAn[3].HinhAnh;
                    pic_D.Visible = true;
                    Link_ClearD.Visible = true;
                }
                txtDapanE.Text = DsdapAn[4].NoiDung;
                MaCauE = DsdapAn[4].MaDapAn;
                if (DsdapAn[4].LoaiDA == 1)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                if (DsdapAn[4].HinhAnh == "" || DsdapAn[4].HinhAnh == null)
                {
                    Link_ClearE.Visible = false;
                    pic_E.Visible = false;
                }
                else
                {
                    pic_E.ImageLocation = DsdapAn[4].HinhAnh;
                    pic_E.Visible = true;
                    Link_ClearE.Visible = true;
                }
            }
            if (soDapan == 6)
            {
                pnlThem.Visible = false;

                txtDapanA.Text = DsdapAn[0].NoiDung;
                MaCauA = DsdapAn[0].MaDapAn;
                if (DsdapAn[0].LoaiDA == 1)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                if (DsdapAn[0].HinhAnh == "" || DsdapAn[0].HinhAnh == null)
                {
                    Link_ClearA.Visible = false;
                    pic_A.Visible = false;
                }
                else
                {
                    pic_A.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_A.Visible = true;
                    Link_ClearA.Visible = true;
                }
                txtDapanB.Text = DsdapAn[1].NoiDung;
                MaCauB = DsdapAn[1].MaDapAn;
                if (DsdapAn[1].LoaiDA == 1)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                if (DsdapAn[1].HinhAnh == "" || DsdapAn[1].HinhAnh == null)
                {
                    Link_ClearB.Visible = false;
                    pic_B.Visible = false;
                }
                else
                {
                    pic_B.ImageLocation = DsdapAn[1].HinhAnh;
                    pic_B.Visible = true;
                    Link_ClearB.Visible = true;
                }
                txtDapanC.Text = DsdapAn[2].NoiDung;
                MaCauC = DsdapAn[2].MaDapAn;
                if (DsdapAn[2].LoaiDA == 1)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                if (DsdapAn[2].HinhAnh == "" || DsdapAn[2].HinhAnh == null)
                {
                    Link_ClearC.Visible = false;
                    pic_C.Visible = false;
                }
                else
                {
                    pic_C.ImageLocation = DsdapAn[2].HinhAnh;
                    pic_C.Visible = true;
                    Link_ClearC.Visible = true;
                }
                txtDapanD.Text = DsdapAn[3].NoiDung;
                MaCauD = DsdapAn[3].MaDapAn;
                if (DsdapAn[3].LoaiDA == 1)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                if (DsdapAn[3].HinhAnh == "" || DsdapAn[3].HinhAnh == null)
                {
                    Link_ClearD.Visible = false;
                    pic_D.Visible = false;
                }
                else
                {
                    pic_D.ImageLocation = DsdapAn[3].HinhAnh;
                    pic_D.Visible = true;
                    Link_ClearD.Visible = true;
                }
                txtDapanE.Text = DsdapAn[4].NoiDung;
                MaCauE = DsdapAn[4].MaDapAn;
                if (DsdapAn[4].LoaiDA == 1)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                if (DsdapAn[4].HinhAnh == "" || DsdapAn[4].HinhAnh == null)
                {
                    Link_ClearE.Visible = false;
                    pic_E.Visible = false;
                }
                else
                {
                    pic_E.ImageLocation = DsdapAn[4].HinhAnh;
                    pic_E.Visible = true;
                    Link_ClearE.Visible = true;
                }
                txtDapanF.Text = DsdapAn[5].NoiDung;
                MaCauF = DsdapAn[5].MaDapAn;
                if (DsdapAn[5].LoaiDA == 1)
                {
                    txtDapanF.ForeColor = Color.Red;
                    chk_CauF.Checked = true;
                }
                else
                {
                    txtDapanF.ForeColor = Color.Black;
                    chk_CauF.Checked = false;
                }
                if (DsdapAn[5].HinhAnh == "" || DsdapAn[5].HinhAnh == null)
                {
                    Link_ClearF.Visible = false;
                    pic_F.Visible = false;
                }
                else
                {
                    pic_F.ImageLocation = DsdapAn[0].HinhAnh;
                    pic_F.Visible = true;
                    Link_ClearF.Visible = true;
                }
            }
        }
        private void frmChinhsuaCauhoi_Load(object sender, EventArgs e)
        {
            
            if (MacauHoi == "")
            {
                RatingDoKho.Rating = 1;
                Hide_TaoMoi();
                lblSolgDapAn.Visible = true;
                cboSolgDapan.Visible = true;
                lblchuong.Visible = true;
                lblMon.Visible = true;
                cboxMonHoc.Visible = true;
                cboChuong.Visible = true;
                load_ComboMonHoc();
                loadcboChuong(cboMa2);
                cboSolgDapan.SelectedIndex = 2;
                cboMin.SelectedIndex = 0;
                
            }
            else
            {
                lblSolgDapAn.Visible = false;
                cboSolgDapan.Visible = false;
                lblchuong.Visible = false;
                lblMon.Visible = false;
                cboxMonHoc.Visible = false;
                cboChuong.Visible = false;
                Load_DapAn();
                Load_Noidung();
            }
            
        }
        int ThuTu = 0;
        List<string> DsDapAnXoa = new List<string>();
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (pnlDapanC.Visible == false)
            {

                ThuTu = 3;
                pnlDapanC.Visible = true;
                txtDapanC.Text = "";
                chk_CauC.Checked = false;
                txtDapanC.ForeColor = Color.Black;
                pic_C.Visible = false;
                Link_ClearC.Visible = false;
                MaCauC = "0";
                pic_C.ImageLocation = "";
                return;
            }
            if (pnlDapanD.Visible == false)
            {
                ThuTu = 4;
                MaCauD = "0";
                pnlDapanD.Visible = true;
                txtDapanD.Text = "";
                chk_CauD.Checked = false;
                txtDapanD.ForeColor = Color.Black;
                pic_D.Visible = false;
                Link_ClearD.Visible = false;
                pic_D.ImageLocation = "";
                return;
            }
            if (pnlDapanE.Visible == false)
            {
                pic_E.ImageLocation = "";
                ThuTu = 5;
                MaCauE = "0";
                pnlDapanE.Visible = true;
                txtDapanE.Text = "";
                chk_CauE.Checked = false;
                txtDapanE.ForeColor = Color.Black;
                pic_E.Visible = false;
                Link_ClearE.Visible = false;
                return;
            }
            if (pnlDapanF.Visible == false)
            {
                pic_F.ImageLocation = "";
                ThuTu = 6;
                MaCauF = "0";
                pnlDapanF.Visible = true;
                pnlThem.Hide();
                txtDapanF.Text = "";
                chk_CauF.Checked = false;
                txtDapanF.ForeColor = Color.Black;
                pic_F.Visible = false;
                Link_ClearF.Visible = false;
                return;
            }
        }

        private void RatingDoKho_RatingChanged(object sender, EventArgs e)
        {
            if (RatingDoKho.Rating == 0)
                return;
            if (RatingDoKho.Rating == 1)
            {
                lblDokho.Text = "Rất dễ";
            }
            if (RatingDoKho.Rating == 2)
            {
                lblDokho.Text = "Dễ";
            }
            if (RatingDoKho.Rating == 3)
            {
                lblDokho.Text = "Vừa";
            }
            if (RatingDoKho.Rating == 4)
            {
                lblDokho.Text = "Khó";
            }
            if (RatingDoKho.Rating == 5)
            {
                lblDokho.Text = "Rất khó";
            }
        }

        private void cboSolgcauhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboSolgDapan.Text == "") return;
            if (cboSolgDapan.Text == "2")
            {
                pnlDapanC.Visible = false;
                pnlDapanD.Visible = false;
                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;
                return;
            }
            if (cboSolgDapan.Text == "3")
            {
                pnlDapanC.Visible = true;
                txtDapanC.Text = "";
                pic_C.ImageLocation = "";
                pic_C.Visible = false;
                Link_ClearC.Visible = false;
                chk_CauC.Checked = false;

                pnlDapanD.Visible = false;
                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;
                return;
            }
            if (cboSolgDapan.Text == "4")
            {
                pnlDapanC.Visible = true;
                txtDapanC.Text = "";
                pic_C.ImageLocation = "";
                pic_C.Visible = false;
                Link_ClearC.Visible = false;
                chk_CauC.Checked = false;

                pnlDapanD.Visible = true;
                txtDapanD.Text = "";
                pic_D.ImageLocation = "";
                pic_D.Visible = false;
                Link_ClearD.Visible = false;
                chk_CauD.Checked = false;

                pnlDapanE.Visible = false;
                pnlDapanF.Visible = false;
                return;
            }
            if (cboSolgDapan.Text == "5")
            {
                pnlDapanC.Visible = true;
                txtDapanC.Text = "";
                pic_C.ImageLocation = "";
                pic_C.Visible = false;
                Link_ClearC.Visible = false;
                chk_CauC.Checked = false;

                pnlDapanD.Visible = true;
                txtDapanD.Text = "";
                pic_D.ImageLocation = "";
                pic_D.Visible = false;
                Link_ClearD.Visible = false;
                chk_CauD.Checked = false;

                pnlDapanE.Visible = true;
                txtDapanE.Text = "";
                pic_E.ImageLocation = "";
                pic_E.Visible = false;
                Link_ClearE.Visible = false;
                chk_CauE.Checked = false;

                pnlDapanF.Visible = false;
                return;
            }
            if (cboSolgDapan.Text == "6")
            {
                pnlDapanC.Visible = true;
                txtDapanC.Text = "";
                pic_C.ImageLocation = "";
                pic_C.Visible = false;
                Link_ClearC.Visible = false;
                chk_CauC.Checked = false;

                pnlDapanD.Visible = true;
                txtDapanD.Text = "";
                pic_D.ImageLocation = "";
                pic_D.Visible = false;
                Link_ClearD.Visible = false;
                chk_CauD.Checked = false;

                pnlDapanE.Visible = true;
                txtDapanE.Text = "";
                pic_E.ImageLocation = "";
                pic_E.Visible = false;
                Link_ClearE.Visible = false;
                chk_CauE.Checked = false;

                pnlDapanF.Visible = true;
                txtDapanF.Text = "";
                pic_F.ImageLocation = "";
                pic_F.Visible = false;
                Link_ClearF.Visible = false;
                chk_CauF.Checked = false;
                return;
            }
        }

        private void chk_CauA_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauA.Checked == true)
            {
                txtDapanA.ForeColor = Color.Red;
            }
            else
            {
                txtDapanA.ForeColor = Color.Black;
            }
        }

        private void chk_CauB_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauB.Checked == true)
            {
                txtDapanB.ForeColor = Color.Red;
            }
            else
            {
                txtDapanB.ForeColor = Color.Black;
            }
        }

        private void chk_CauC_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauC.Checked == true)
            {
                txtDapanC.ForeColor = Color.Red;
            }
            else
            {
                txtDapanC.ForeColor = Color.Black;
            }
        }

        private void chk_CauD_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauD.Checked == true)
            {
                txtDapanD.ForeColor = Color.Red;
            }
            else
            {
                txtDapanD.ForeColor = Color.Black;
            }
        }

        private void chk_CauE_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauE.Checked == true)
            {
                txtDapanE.ForeColor = Color.Red;
            }
            else
            {
                txtDapanE.ForeColor = Color.Black;
            }
        }

        private void chk_CauF_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_CauF.Checked == true)
            {
                txtDapanF.ForeColor = Color.Red;
            }
            else
            {
                txtDapanF.ForeColor = Color.Black;
            }
        }

        private void btn_XoaF_Click(object sender, EventArgs e)
        {
            if (ThuTu == 6)
            {
                pnlThem.Show();
                pnlDapanF.Visible = false;
                ThuTu = 0;
                return;
            }
            if (MaCauF != "" && MaCauF != "0")
            {

                DsDapAnXoa.Add(MaCauF);
                MaCauF = "";
                pnlDapanF.Visible = false;
                return;
            }
        }

        private void btn_XoaE_Click(object sender, EventArgs e)
        {
            if (ThuTu == 5)
            {
                pnlDapanE.Visible = false;
                ThuTu = 0;
                return;
            }
            if (MaCauE != "" && MaCauE != "0")
                DsDapAnXoa.Add(MaCauE);
            if (MaCauF != "")
            {
                MaCauE = MaCauF;
                txtDapanE.Text = txtDapanF.Text;
                if (chk_CauF.Checked == true)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                if(pic_F.ImageLocation!=null &&pic_F.ImageLocation!=""){
                pic_E.ImageLocation = pic_F.ImageLocation;
                pic_F.ImageLocation = "";
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
                }
                else
                {
                    pic_E.Visible = false;
                    Link_ClearE.Visible = false;
                }
                MaCauF = "";
                pnlThem.Visible = true;
                pnlDapanF.Visible = false;
                return;
            }
            if (MaCauE != "")
            {
                pnlDapanE.Visible = false;
                MaCauE = "";
                return;
            }
        }

        private void btn_XoaD_Click(object sender, EventArgs e)
        {
            if (ThuTu == 4)
            {
                pnlDapanD.Visible = false;
                ThuTu = 0;
                return;
            }
            if (MaCauD != "" && MaCauD != "0")
            DsDapAnXoa.Add(MaCauD);
            if (MaCauF != "")
            {
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                pic_E.ImageLocation = "";
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }

                MaCauE = MaCauF;
                if (pic_F.ImageLocation != null && pic_F.ImageLocation != ""){
                pic_E.ImageLocation = pic_F.ImageLocation;
                pic_F.ImageLocation = "";
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
                }
                else
                {
                    pic_E.Visible = false;
                    Link_ClearE.Visible = false;
                }
                txtDapanE.Text = txtDapanF.Text;
                if (chk_CauF.Checked == true)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }

                MaCauF = "";
                pnlDapanF.Visible = false;
                pnlThem.Visible = true;
                return;
            }
            if (MaCauE != "")
            {
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != "")
                {
                    pic_D.ImageLocation = pic_E.ImageLocation;
                    pic_E.ImageLocation = "";
                    pic_D.Visible = true;
                    Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = "";
                pnlDapanE.Visible = false;
                return;
            }
            if (MaCauD != "")
            {
                pnlDapanD.Visible = false;
                MaCauD = "";
                return;
            }
        }

        private void btn_XoaC_Click(object sender, EventArgs e)
        {
            if (ThuTu == 3)
            {
                pnlDapanC.Visible = false;
                ThuTu = 0;
                return;
            }
            if (MaCauC != "" && MaCauC != "0")
            DsDapAnXoa.Add(MaCauC);
            if (MaCauF != "")
            {
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != "")
                {
                    pic_C.ImageLocation = pic_D.ImageLocation;
                    pic_D.ImageLocation = "";
                    pic_C.Visible = true;
                    Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                    pic_D.ImageLocation = pic_E.ImageLocation;
                    pic_E.ImageLocation = "";
                    pic_D.Visible = true;
                    Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = MaCauF;
                if (pic_F.ImageLocation != null && pic_F.ImageLocation != ""){
                pic_E.ImageLocation = pic_F.ImageLocation;
                pic_F.ImageLocation = "";
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
                }
                else
                {
                    pic_E.Visible = false;
                    Link_ClearE.Visible = false;
                }
                txtDapanE.Text = txtDapanF.Text;
                if (chk_CauF.Checked == true)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                MaCauF = "";
                pnlDapanF.Visible = false;
                pnlThem.Visible = true;
                return;
            }
            if (MaCauE != "")
            {
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = "";
                pnlDapanE.Visible = false;
                return;
            }
            if (MaCauD != "")
            {
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                
                MaCauD = "";
                pnlDapanD.Visible = false;
                return;
            }
            if (MaCauC != "")
            {
                pnlDapanC.Visible = false;
                MaCauC = "";
                return;
            }
        }
        public int Count_DapAn(){
            int x = 0;
            if (pnlDapanA.Visible == true)
                x++;
            if (pnlDapanB.Visible == true)
                x++;
            if (pnlDapanC.Visible == true)
                x++;
            if (pnlDapanD.Visible == true)
                x++;
            if (pnlDapanE.Visible == true)
                x++;
            if (pnlDapanF.Visible == true)
                x++;
            return x;
        }
        private void btn_XoaB_Click(object sender, EventArgs e)
        {
            if (Count_DapAn() == 2)
            {
                MessageBox.Show("Không xoá được. Tối thiểu là 2 đáp án"); 
                return;
            }
            if (MaCauB != "" && MaCauB != "0")
            DsDapAnXoa.Add(MaCauB);
            if (MaCauF != "")
            {
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = MaCauF;
                if (pic_F.ImageLocation != null && pic_F.ImageLocation != ""){
                pic_E.ImageLocation = pic_F.ImageLocation;
                pic_F.ImageLocation = "";
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
                }
                else
                {
                    pic_E.Visible = false;
                    Link_ClearE.Visible = false;
                }
                txtDapanE.Text = txtDapanF.Text;
                if (chk_CauF.Checked == true)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                MaCauF = "";
                pnlDapanF.Visible = false;
                pnlThem.Visible = true;
                return;
            }
            if (MaCauE != "")
            {
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = "";
                pnlDapanE.Visible = false;
                return;
            }
            if (MaCauD != "")
            {
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                    
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }

                MaCauD = "";
                pnlDapanD.Visible = false;
                return;
            }
            if (MaCauC != "")
            {
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                pnlDapanC.Visible = false;
                MaCauC = "";
                return;
            }
        }

        private void btn_XoaA_Click(object sender, EventArgs e)
        {

            if (Count_DapAn() == 2)
            {
                MessageBox.Show("Không xoá được. Tối thiểu là 2 đáp án");
                return;
            }
            if (MaCauA != "" && MaCauA != "0")
            DsDapAnXoa.Add(MaCauA);
            if (MaCauF != "")
            {
                MaCauA = MaCauB;
                if (pic_B.ImageLocation != null && pic_B.ImageLocation != ""){
                pic_A.ImageLocation = pic_B.ImageLocation;
                pic_B.ImageLocation = "";
                pic_A.Visible = true;
                Link_ClearA.Visible = true;
                }
                else
                {
                    pic_A.Visible = false;
                    Link_ClearA.Visible = false;
                }
                txtDapanA.Text = txtDapanB.Text;
                if (chk_CauB.Checked == true)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = MaCauF;
                if (pic_F.ImageLocation != null && pic_F.ImageLocation != ""){
                pic_E.ImageLocation = pic_F.ImageLocation;
                pic_F.ImageLocation = "";
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
                }
                else
                {
                    pic_E.Visible = false;
                    Link_ClearE.Visible = false;
                }
                txtDapanE.Text = txtDapanF.Text;
                if (chk_CauF.Checked == true)
                {
                    txtDapanE.ForeColor = Color.Red;
                    chk_CauE.Checked = true;
                }
                else
                {
                    txtDapanE.ForeColor = Color.Black;
                    chk_CauE.Checked = false;
                }
                MaCauF = "";
                pnlDapanF.Visible = false;
                pnlThem.Visible = true;
                return;
            }
            if (MaCauE != "")
            {
                MaCauA = MaCauB;
                if (pic_B.ImageLocation != null && pic_B.ImageLocation != ""){
                pic_A.ImageLocation = pic_B.ImageLocation;
                pic_B.ImageLocation = "";
                pic_A.Visible = true;
                Link_ClearA.Visible = true;
                }
                else
                {
                    pic_A.Visible = false;
                    Link_ClearA.Visible = false;
                }
                txtDapanA.Text = txtDapanB.Text;
                if (chk_CauB.Checked == true)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }
                MaCauD = MaCauE;
                if (pic_E.ImageLocation != null && pic_E.ImageLocation != ""){
                pic_D.ImageLocation = pic_E.ImageLocation;
                pic_E.ImageLocation = "";
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
                }
                else
                {
                    pic_D.Visible = false;
                    Link_ClearD.Visible = false;
                }
                txtDapanD.Text = txtDapanE.Text;
                if (chk_CauE.Checked == true)
                {
                    txtDapanD.ForeColor = Color.Red;
                    chk_CauD.Checked = true;
                }
                else
                {
                    txtDapanD.ForeColor = Color.Black;
                    chk_CauD.Checked = false;
                }
                MaCauE = "";
                pnlDapanE.Visible = false;
                return;
            }
            if (MaCauD != "")
            {
                MaCauA = MaCauB;
                if (pic_B.ImageLocation != null && pic_B.ImageLocation != ""){
                pic_A.ImageLocation = pic_B.ImageLocation;
                pic_B.ImageLocation = "";
                pic_A.Visible = true;
                Link_ClearA.Visible = true;
                }
                else
                {
                    pic_A.Visible = false;
                    Link_ClearA.Visible = false;
                }
                txtDapanA.Text = txtDapanB.Text;
                if (chk_CauB.Checked == true)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                MaCauC = MaCauD;
                if (pic_D.ImageLocation != null && pic_D.ImageLocation != ""){
                pic_C.ImageLocation = pic_D.ImageLocation;
                pic_D.ImageLocation = "";
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
                }
                else
                {
                    pic_C.Visible = false;
                    Link_ClearC.Visible = false;
                }
                txtDapanC.Text = txtDapanD.Text;
                if (chk_CauD.Checked == true)
                {
                    txtDapanC.ForeColor = Color.Red;
                    chk_CauC.Checked = true;
                }
                else
                {
                    txtDapanC.ForeColor = Color.Black;
                    chk_CauC.Checked = false;
                }

                MaCauD = "";
                pnlDapanD.Visible = false;
                return;
            }
            if (MaCauC != "")
            {
                MaCauA = MaCauB;
                if (pic_B.ImageLocation != null && pic_B.ImageLocation != ""){
                pic_A.ImageLocation = pic_B.ImageLocation;
                pic_B.ImageLocation = "";
                pic_A.Visible = true;
                Link_ClearA.Visible = true;
                }
                else
                {
                    pic_A.Visible = false;
                    Link_ClearA.Visible = false;
                }
                txtDapanA.Text = txtDapanB.Text;
                if (chk_CauB.Checked == true)
                {
                    txtDapanA.ForeColor = Color.Red;
                    chk_CauA.Checked = true;
                }
                else
                {
                    txtDapanA.ForeColor = Color.Black;
                    chk_CauA.Checked = false;
                }
                MaCauB = MaCauC;
                if (pic_C.ImageLocation != null && pic_C.ImageLocation != ""){
                pic_B.ImageLocation = pic_C.ImageLocation;
                pic_C.ImageLocation = "";
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
                }
                else
                {
                    pic_B.Visible = false;
                    Link_ClearB.Visible = false;
                }
                txtDapanB.Text = txtDapanC.Text;
                if (chk_CauC.Checked == true)
                {
                    txtDapanB.ForeColor = Color.Red;
                    chk_CauB.Checked = true;
                }
                else
                {
                    txtDapanB.ForeColor = Color.Black;
                    chk_CauB.Checked = false;
                }
                pnlDapanC.Visible = false;
                MaCauC = "";
                return;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (cboMin.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn thời gian", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (RatingDoKho.Rating == 0)
            {
                MessageBox.Show("Bạn chưa chọn độ khó", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtNoidung.Text.Trim().Length== 0)
            {
                MessageBox.Show("Bạn chưa nhập nội dung câu hỏi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNoidung.Focus();
                return;
            }
            int dem = 0;
            if (pnlDapanA.Visible==true)
            {
                if (chk_CauA.Checked == true)
                dem++;
                if (txtDapanA.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanA.Focus();
                    return;
                }
            }
            if (pnlDapanB.Visible == true)
            {
                if (chk_CauB.Checked == true)
                dem++;
                if (txtDapanB.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanB.Focus();
                    return;
                }
            }
            if (pnlDapanC.Visible == true)
            {
                if (chk_CauC.Checked == true)
                dem++;
                if (txtDapanC.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanC.Focus();
                    return;
                }
            }
            if (pnlDapanD.Visible == true)
            {
                if (chk_CauD.Checked == true)
                dem++;
                if (txtDapanD.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanD.Focus();
                    return;
                }
            }
            if (pnlDapanE.Visible == true)
            {
                if (chk_CauE.Checked == true)
                dem++;
                if (txtDapanE.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanE.Focus();
                    return;
                }
            }
            if (pnlDapanF.Visible == true)
            {
                if (chk_CauF.Checked == true)
                dem++;
                if (txtDapanF.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Đáp án chưa có nội dung", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDapanF.Focus();
                    return;
                }
            }
            if (dem < 1 )
            {
                MessageBox.Show("Bạn chưa chọn đáp án", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MacauHoi != "")
            {
                DapAnBll da = new DapAnBll();
                if (DsDapAnXoa.Count > 0)
                {
                    for (int i = 0; i < DsDapAnXoa.Count; i++)
                    {
                        da.XoaDapAn(DsDapAnXoa[i].ToString());
                    }
                }
                if (MaCauF == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanF.Text;
                    if (chk_CauF.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_F.ImageLocation != "" && pic_F.ImageLocation != null)
                    {
                        dapan.HinhAnh= bll.CopyPicture(pic_F.ImageLocation,"DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                if (MaCauE == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanE.Text;
                    if (chk_CauE.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_E.ImageLocation != "" && pic_E.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_E.ImageLocation,"DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                if (MaCauD == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanD.Text;
                    if (chk_CauD.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_D.ImageLocation != "" && pic_D.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_D.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                
                if (MaCauC == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanC.Text;
                    if (chk_CauC.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_C.ImageLocation != "" && pic_C.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_C.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                if (MaCauB == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanB.Text;
                    if (chk_CauB.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_B.ImageLocation != "" && pic_B.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_B.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                if (MaCauA == "0")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanA.Text;
                    if (chk_CauA.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_A.ImageLocation != "" && pic_A.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_A.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = MacauHoi;
                    dabll.ThemDapAn(dapan);
                }
                if (MaCauA != "0" && MaCauA != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauA;
                    dapan.NoiDung = txtDapanA.Text;
                    if (chk_CauA.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_A.ImageLocation != "" && pic_A.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_A.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture(dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                if (MaCauB != "0" && MaCauB != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauB;
                    dapan.NoiDung = txtDapanB.Text;
                    if (chk_CauB.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_B.ImageLocation != "" && pic_B.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_B.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture( dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                if (MaCauC != "0" && MaCauC != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauC;
                    dapan.NoiDung = txtDapanC.Text;
                    if (chk_CauC.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_C.ImageLocation != "" && pic_C.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_C.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture(dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                if (MaCauD != "0" && MaCauD != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauD;
                    dapan.NoiDung = txtDapanD.Text;
                    if (chk_CauD.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_D.ImageLocation != "" && pic_D.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_D.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture( dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                if (MaCauE != "0" && MaCauE != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauE;
                    dapan.NoiDung = txtDapanE.Text;
                    if (chk_CauE.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_E.ImageLocation != "" && pic_E.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_E.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture( dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                if (MaCauF != "0" && MaCauF != "")
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    dapan.MaDapAn = MaCauF;
                    dapan.NoiDung = txtDapanF.Text;
                    if (chk_CauF.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_F.ImageLocation != "" && pic_F.ImageLocation != null)
                    {
                        dapan.HinhAnh = BLL.CopyPicture(pic_F.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        if (dabll.PictureLink(dapan.MaDapAn) != "")
                            BLL.XoaPicture(dabll.PictureLink(dapan.MaDapAn));
                        dapan.HinhAnh = "";
                    }
                    dabll.SuaDapAn(dapan);
                }
                CauHoiBll ch = new CauHoiBll();
                CauHoiTbl Cauhoi = new CauHoiTbl();
                Cauhoi.MaCauHoi = MacauHoi;
                Cauhoi.NoiDung = txtNoidung.Text;
                Cauhoi.ThoiGian = int.Parse( cboMin.Text);
                Cauhoi.DoKho = RatingDoKho.Rating;
                BusinessLogicLayer BLL1 = new BusinessLogicLayer();
                if (pic_Noidung.ImageLocation != "" && pic_Noidung.ImageLocation != null)
                {
                    Cauhoi.HinhAnh = BLL1.CopyPicture(pic_Noidung.ImageLocation, "CAUHOI", Cauhoi.MaCauHoi);
                }
                else
                {
                    if (ch.PictureLink(Cauhoi.MaCauHoi) != "")
                        BLL1.XoaPicture(ch.PictureLink(Cauhoi.MaCauHoi));
                    Cauhoi.HinhAnh = "";
                }
                ch.SuaCauHoi(Cauhoi);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                
            }
            else
            {
                if (cboChuong.Text == "")
                {
                    MessageBox.Show("Chưa chọn Chương hoặc Phải tạo Chương trước!");
                    return;
                }
                    CauHoiBll cauhoibll= new CauHoiBll();
                    CauHoiTbl cauhoi= new CauHoiTbl();
                    BusinessLogicLayer bll = new BusinessLogicLayer();
                    string Random = bll.RandomMa();
                    while (cauhoibll.KiemTra_TrungKhoa(Random) == true)
                    {
                        Random = bll.RandomMa();
                    }
                    cauhoi.MaCauHoi = Random;
                    cauhoi.NoiDung = txtNoidung.Text;
                    cauhoi.ThoiGian = int.Parse(cboMin.Text);
                    cauhoi.DoKho = RatingDoKho.Rating;
                    BusinessLogicLayer BLL = new BusinessLogicLayer();
                    if (pic_Noidung.ImageLocation != "" && pic_Noidung.ImageLocation != null)
                    {
                        cauhoi.HinhAnh = BLL.CopyPicture(pic_Noidung.ImageLocation, "CAUHOI", cauhoi.MaCauHoi);
                    }
                    else
                    {
                        cauhoi.HinhAnh = "";
                    }

                    ChuongBll chg = new ChuongBll();
                    MonHocBLL mh=new MonHocBLL();
                    cauhoi.MaChuong = ((ChuongTbl)cboChuong.SelectedItem).MaChuong;
                    cauhoibll.ThemCauHoi(cauhoi);
                    
                if (pnlDapanA.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanA.Text;
                    if (chk_CauA.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_A.ImageLocation != "" && pic_A.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_A.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                if (pnlDapanB.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanB.Text;
                    if (chk_CauB.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                   if (pic_B.ImageLocation != "" && pic_B.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_B.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                if (pnlDapanC.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanC.Text;
                    if (chk_CauC.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_C.ImageLocation != "" && pic_C.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_C.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                if (pnlDapanD.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanD.Text;
                    if (chk_CauD.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_D.ImageLocation != "" && pic_D.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_D.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                if (pnlDapanE.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanE.Text;
                    if (chk_CauE.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                   if (pic_E.ImageLocation != "" && pic_E.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_E.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                if (pnlDapanF.Visible == true)
                {
                    DapAnBll dabll = new DapAnBll();
                    DapAnTbl dapan = new DapAnTbl();
                    bll = new BusinessLogicLayer();
                    string k = bll.RandomMa();
                    while (dabll.KiemTra_TrungKhoa(k) == true)
                    {
                        k = bll.RandomMa();
                    }
                    dapan.MaDapAn = k;
                    dapan.NoiDung = txtDapanF.Text;
                    if (chk_CauF.Checked == true)
                        dapan.LoaiDA = 1;
                    else
                        dapan.LoaiDA = 0;
                    if (pic_F.ImageLocation != "" && pic_F.ImageLocation != null)
                    {
                        dapan.HinhAnh = bll.CopyPicture(pic_F.ImageLocation, "DAPAN", dapan.MaDapAn);
                    }
                    else
                    {
                        dapan.HinhAnh = "";
                    }
                    dapan.MaCauHoi = Random;
                    dabll.ThemDapAn(dapan);
                }
                MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNoidung.Text = "";
                txtDapanA.Text = "";
                txtDapanB.Text = "";
                txtDapanC.Text = "";
                txtDapanD.Text = "";
                txtDapanE.Text = "";
                txtDapanF.Text = "";
                chk_CauA.Checked = false;
                chk_CauB.Checked = false;
                chk_CauC.Checked = false;
                chk_CauD.Checked = false;
                chk_CauE.Checked = false;
                chk_CauF.Checked = false;

                pic_A.ImageLocation = "";
                pic_A.Visible = false;
                Link_ClearA.Visible = false;

                pic_B.ImageLocation = "";
                pic_B.Visible = false;
                Link_ClearB.Visible = false;

                pic_C.ImageLocation = "";
                pic_C.Visible = false;
                Link_ClearC.Visible = false;

                pic_D.ImageLocation = "";
                pic_D.Visible = false;
                Link_ClearD.Visible = false;

                pic_E.ImageLocation = "";
                pic_E.Visible = false;
                Link_ClearE.Visible = false;

                pic_F.ImageLocation = "";
                pic_F.Visible = false;
                Link_ClearF.Visible = false;

                pic_Noidung.ImageLocation = "";
                pic_Noidung.Visible = false;
                Link_ClearNoidung.Visible = false;
                RatingDoKho.Rating = 1;
                cboMin.SelectedIndex = 0;
            }
        }
        public void load_ComboMonHoc()
        {
            cboxMonHoc.DataSource = null;
            MonHocBLL mh = new MonHocBLL();
            List<MonHocTbl> lst= mh.LayTatCaMonHoc();
            if(lst.Count>0){
                cboxMonHoc.DataSource = lst;
                cboxMonHoc.DisplayMember = "TenMonHoc";
                cboxMonHoc.ValueMember = "maMonHoc";
            }
            cboxMonHoc.SelectedValue = cboMa1;
            
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void cboxMonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadcboChuong("");
        }

        private void loadcboChuong(string s)
        {
            if (cboxMonHoc.Text == "")
                return;
            cboChuong.DataSource = null;
            if (cboxMonHoc.Text == null || cboxMonHoc.Text == "" || cboxMonHoc.DataSource == null)
                return;
            ChuongBll ch = new ChuongBll();
            List<ChuongTbl> DSChuong = new List<ChuongTbl>();
            MonHocBLL mh = new MonHocBLL();

            DSChuong = ch.DSChuong_MaMon(cboxMonHoc.SelectedValue.ToString());
            if (DSChuong.Count > 0)
            {
                cboChuong.DataSource = DSChuong;
                cboChuong.DisplayMember = "TenChuong";
                cboChuong.ValueMember = "MaChuong";
            }
            if(s!="")
            cboChuong.SelectedValue = cboMa2;
            else
                if (DSChuong.Count > 0)
                cboChuong.SelectedIndex = 0;
        }

        private void Link_Noidung_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_Noidung.ImageLocation = open.FileName;
                pic_Noidung.Visible = true;
                Link_ClearNoidung.Visible = true;
            }
            
        }

        private void Link_ClearNoidung_Click(object sender, EventArgs e)
        {
            pic_Noidung.ImageLocation = "";
            pic_Noidung.Visible = false;
            Link_ClearNoidung.Visible = false;
        }

        private void Link_ClearA_Click(object sender, EventArgs e)
        {
            pic_A.ImageLocation = "";
            pic_A.Visible = false;
            Link_ClearA.Visible = false;
        }

        private void Link_ClearB_Click(object sender, EventArgs e)
        {
            pic_B.ImageLocation = "";
            pic_B.Visible = false;
            Link_ClearB.Visible = false;
        }

        private void Link_ClearC_Click(object sender, EventArgs e)
        {
            pic_C.Visible = false;
            Link_ClearC.Visible = false;
            pic_C.ImageLocation = "";
        }

        private void Link_ClearD_Click(object sender, EventArgs e)
        {
            pic_D.ImageLocation = "";
            pic_D.Visible = false;
            Link_ClearD.Visible = false;
        }

        private void Link_ClearE_Click(object sender, EventArgs e)
        {
            pic_E.ImageLocation = "";
            pic_E.Visible = false;
            Link_ClearE.Visible = false;
        }

        private void Link_ClearF_Click(object sender, EventArgs e)
        {
            pic_F.ImageLocation = "";
            pic_F.Visible = false;
            Link_ClearF.Visible = false;
        }

        private void Link_A_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_A.ImageLocation = open.FileName;
                pic_A.Visible = true;
                Link_ClearA.Visible = true;
            }
        }

        private void Link_B_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_B.ImageLocation = open.FileName;
                pic_B.Visible = true;
                Link_ClearB.Visible = true;
            }
        }

        private void Link_C_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_C.ImageLocation = open.FileName;
                pic_C.Visible = true;
                Link_ClearC.Visible = true;
            }
        }

        private void Link_D_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_D.ImageLocation = open.FileName;
                pic_D.Visible = true;
                Link_ClearD.Visible = true;
            }
        }

        private void Link_E_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_E.ImageLocation = open.FileName;
                pic_E.Visible = true;
                Link_ClearE.Visible = true;
            }
        }

        private void Link_F_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "File Image (JPG)|*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pic_F.ImageLocation = open.FileName;
                pic_F.Visible = true;
                Link_ClearF.Visible = true;
            }
        }

        private void pnlDapanC_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pic_B_Click(object sender, EventArgs e)
        {

        }

        private void cboMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cboSec_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cboxMonHoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cboSolgDapan_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cboChuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cboChuong_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        
    }
}